package db;

import exceptions.DatabaseException;
import java.sql.*;
import java.util.Locale;
import javax.naming.*;
import javax.sql.DataSource;

/**
 *
 * @author Alejandro
 */
public class DBConnection {

    public static DBConnection getInstance() {
        try {
            if (instance == null) {
                instance = new DBConnection();
            }
            return instance;
        } catch (Exception e) {
            return null;
        }
    }

    public Connection connect() throws DatabaseException {
        try {
            dbc = ds.getConnection();
        } catch (SQLException ex) {
            throw new DatabaseException(ex);
        }
        return dbc;
    }

    private DBConnection() {
        try {
            Locale.setDefault(Locale.ENGLISH);
            Context c = new InitialContext();
            Object ods = c.lookup("jdbc/myconnection");
            ds = (DataSource) ods;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
    private static DBConnection instance = new DBConnection();
    private static DataSource ds;
    private static Connection dbc;
}
